/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.cholm.media.castdr.loaders;

// import java.util.List;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import org.cholm.media.castdr.LogUtils;
import org.cholm.media.castdr.backend.Extractor;
import org.cholm.media.castdr.backend.Serie;
import org.cholm.media.castdr.backend.SubCategory;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static org.cholm.media.castdr.LogUtils.LOGd;

/**
 * Loader of series
 */
public class SerieLoader extends AsyncTaskLoader<List<Serie>> {

    public static final String TAG = LogUtils.makeLogTag(SerieLoader.class);
    private List<Serie> mData = new ArrayList<Serie>();
    private SubCategory mSub = null;

    public SerieLoader(Context context, SubCategory sub) {
        super(context);
        mSub = sub;
        LOGd(TAG, "Created loader");
    }

    protected void addGroup(Serie s) {
        LOGd(TAG, "Adding serie " + s.getTitle());
        mData.add(s);
    }

    @Override
    public List<Serie> loadInBackground() {
        LOGd(TAG, "Loading in background from " + mSub.getUrl());
        if (!mSub.load()) {
            mData = null;
        } else {
            LOGd(TAG, "Got a total of " + mSub.getMap().size()
                    + " series for " + mSub.getName());
            for (Serie s : mSub.getMap().values())
                mData.add(s);
            Collections.sort(mData, new Comparer());
        }

        return mData;
    }

    @Override
    protected void onStartLoading() {
        LOGd(TAG, "Starting load");
        super.onStartLoading();
        forceLoad();
    }

    @Override
    protected void onReset() {
        LOGd(TAG, "Clearing data");
        // mData.clear();
    }

    @Override
    protected void onForceLoad() {
        LOGd(TAG, "Force loading");
        // mData.clear();
        super.onForceLoad();
    }

    /**
     * Handles a request to stop the Loader.
     */
    @Override
    protected void onStopLoading() {
        // Attempt to cancel the current load task if possible.
        LOGd(TAG, "Stopping load");
        cancelLoad();
    }

    private class Comparer implements Comparator<Serie> {
        private Collator mC = Collator.getInstance(Extractor.DANISH);

        Comparer() {
            mC.setStrength(Collator.SECONDARY);
        }

        @Override
        public int compare(Serie lhs, Serie rhs) {
            return mC.compare(lhs.getTitle(), rhs.getTitle());
        }
    }
}
/* EOF */

